﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Data;
using System.Reflection;

namespace BAL
{
    public class ConvertUtility
    {
        /// <summary>
        /// Formats the time vn (dd-mm-yy).
        /// </summary>
        /// <param name="dt">datetime.</param>
        /// <param name="defaultText">Default value.</param>
        /// <returns></returns>
        public static string FormatTimeVn(DateTime dt, string defaultText)
        {
            if (ToDateTime(dt) != new DateTime(1900, 1, 1))
                return dt.ToString("dd-mm-yy");
            return defaultText;
        }

        public static DateTime ToDateTime(object obj)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return DateTime.Now;

            return retVal;
        }

        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                return defaultValue;
            }
            if (retVal == new DateTime(1, 1, 1)) return defaultValue;

            return retVal;
        }

        public static DateTime VNToDateTime(string obj, DateTime defaultValue)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj, new CultureInfo("vi-VN"));
            }
            catch
            {
                return defaultValue;
            }
            if (retVal == new DateTime(1, 1, 1)) return defaultValue;

            return retVal;
        }

        public static string ToMoney(decimal str, string money)
        {
            string retVal = str.ToString("N", CultureInfo.CreateSpecificCulture("en-US"));
            retVal = retVal.Substring(0, retVal.Length - 3);
            retVal += money;
            return retVal;
        }

        //public static string DataTableToJSON(DataTable table)
        //{
        //    List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

        //    foreach (DataRow row in table.Rows)
        //    {
        //        Dictionary<string, object> dict = new Dictionary<string, object>();

        //        foreach (DataColumn col in table.Columns)
        //        {
        //            dict[col.ColumnName] = row[col];
        //        }
        //        list.Add(dict);
        //    }
        //    JavaScriptSerializer serializer = new JavaScriptSerializer();
        //    return serializer.Serialize(list);
        //}

        //public static DataTable ConvertTo<T>(IList<T> list)
        //{
        //    DataTable table = CreateTable<T>();
        //    Type entityType = typeof(T);
        //    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

        //    foreach (T item in list)
        //    {
        //        DataRow row = table.NewRow();

        //        foreach (PropertyDescriptor prop in properties)
        //        {
        //            row[prop.Name] = prop.GetValue(item);
        //        }

        //        table.Rows.Add(row);
        //    }

        //    return table;
        //}

        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;

            if (rows != null)
            {
                list = new List<T>();

                foreach (DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        public static IList<T> ConvertTo<T>(DataTable table)
        {
            if (table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();

            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            return ConvertTo<T>(rows);
        }

        public static T CreateItem<T>(DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance<T>();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        prop.SetValue(obj, value, null);
                    }
                    catch
                    {
                        // You can log something here
                        throw;
                    }
                }
            }

            return obj;
        }

        //public static DataTable CreateTable<T>()
        //{
        //    Type entityType = typeof(T);
        //    DataTable table = new DataTable(entityType.Name);
        //    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

        //    foreach (PropertyDescriptor prop in properties)
        //    {
        //        table.Columns.Add(prop.Name, prop.PropertyType);
        //    }

        //    return table;
        //}

        public static int BooleanToInt(bool obj)
        {
            int retVal;

            try
            {
                retVal = obj ? 1 : 0;
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }


        /// <summary>
        /// The convert to
        /// Author: SonPN
        /// CreatedDate: 7/11/2013 9:10 AM
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static T ConvertTo<T>(object obj)
        {
            if (obj == null)
            {
                return default(T);
            }

            try
            {
                var t = (T)System.Convert.ChangeType(obj, typeof(T));
                return t;
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// The convert to
        /// Author: SonPN
        /// CreatedDate: 7/11/2013 9:50 AM
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static T ConvertTo<T>(object obj, object defaultValue)
        {
            if (obj == null)
            {
                return ConvertTo<T>(defaultValue);
            }

            try
            {
                var t = (T)System.Convert.ChangeType(obj, typeof(T));
                return t;
            }
            catch
            {
                return ConvertTo<T>(defaultValue);
            }
        }

        /// <summary>
        /// The convert to
        /// Author: SonPN
        /// CreatedDate: 7/11/2013 9:10 AM
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static T ConvertTo<T>(string obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                return default(T);
            }

            try
            {
                var t = (T)System.Convert.ChangeType(obj, typeof(T));
                return t;
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// The convert to
        /// Author: SonPN
        /// CreatedDate: 7/11/2013 9:55 AM
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">The obj.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static T ConvertTo<T>(string obj, object defaultValue)
        {
            if (string.IsNullOrEmpty(obj))
            {
                return ConvertTo<T>(defaultValue);
            }

            try
            {
                var t = (T)System.Convert.ChangeType(obj, typeof(T));
                return t;
            }
            catch
            {
                return ConvertTo<T>(defaultValue);
            }
        }
    }
}
